<?php
class user extends SO_Controller {
    /*
      Project     : 48c6c450f1a4a0cc53d9585dc0fee742
      Created on  : Mar 16, 2013, 11:29:15 PM
      Author      : Truong Khuong - khuongxuantruong@gmail.com
      Description :
      Purpose of the stylesheet follows.
     */
    function __construct() {
        parent::__construct('auth_users','ause_','id');
        $this->load->model('so/user_model');
    }
    function privilege_view(){
        $this->privilege->aupr_permission = 0;
		if (
                //strpos(",$this->authority,",',View,')!==false 
				!!array_intersect(array('Administrator','Admin'),$this->authoritys)
        ){
			$this->privilege->aupr_permission = 777;
			return true;
		}
		return false;
    }
    function viewport(){
        echo $c=$this->load->view('cms/so/01_user/userViewport',$this->assigns,true);
    }
    function index() {
        $this->viewport();
    }
    function my_profile(){
        echo $c=$this->load->view('so/90_user/userProfile',$this->assigns,true);
    }
    function binding() {
        $result = $this->user_model->binding();
		$this->assigns->data = $result;
        $c=$this->load->view('cms/so/01_user/userList',$this->assigns,true);
        echo $c;die;
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($result));
    }
    function randomstring(){
        $str = random_string('alnum', rand(3, 8)*2);
        $R["result"] = 1;
        $R["message"]=$str;
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($R));
    }
    
    function update_me() {
        $R["result"] = -1;
        $R["message"] = ascii_to_entities("This function to requires an administrative account.<br/>Please check your authority, and try again.");
        $params = $this->input->post("Params");
        if (isset($_SESSION["auth"]["user"])) {
            $u=$_SESSION["auth"]["user"];
            $apri_id = $u->ause_id;
            if(empty($params['ause_password'])){
                unset($params['ause_password']);
            }else{
                $params["ause_password"]=md5($u->ause_username.$params["ause_password"].$u->ause_secretkey);
            }
            $params["ause_update"] = date('Y-m-d H:i:s');
            $rok = @$this->user_model->update($apri_id, $params);
        }
        if ($rok === true) {
            $R["result"] = 1;
            $R["message"] = ascii_to_entities("Data have been save.");
        } else {
            $R["result"] = -1;
            $R["error_number"] = $this->db->_error_number();
            $R["error_message"] = $this->db->_error_message();
            $R["message"] = ascii_to_entities("
                    System rejected your data.<br/>
                    Check your data and try again, Please!<br/>
                    ");
            if ($R["error_number"] != 0)
                $R["message"].=ascii_to_entities("<span class='erc'>[" . $R["error_number"] . "] " . $R["error_message"] . ".</span>");
        }
        
        echo json_encode($R);
    }
    
    function save() {
        $R["result"] = -1;
        $R["message"] = ascii_to_entities("This function to requires an administrative account.<br/>Please check your authority, and try again.");
        if($this->privilege->aupr_permission!=777){echo json_encode($R);die;}
        
        $params = $this->input->post("Params");
        
        $apri_id = $this->input->post("ause_id");
        if ($apri_id != false) {
            $user = $this->user_model->get($apri_id);
            if($user==null){
                $R["result"] = -904;
                $R["message"] = ascii_to_entities("User not exist."); 
                echo json_encode($R);die;
            }
            if(empty($params['ause_password'])){
                unset($params['ause_password']);
            }else{
                $params["ause_password"]=md5($params["ause_username"].$params["ause_password"].$user->ause_secretkey);
            }
            $params["ause_update"] = date('Y-m-d H:i:s');
            $rok = @$this->user_model->update($apri_id, $params);
        } else {
            $params["ause_key"] = random_string('alnum', 8);
            $params["ause_salt"] = random_string('alnum', 8);
            $params["ause_secretkey"] = random_string('alnum', 32);
            $params["ause_password"]=md5($params["ause_username"].$params["ause_password"].$params["ause_secretkey"]);
            $params["ause_position"]=$_SESSION["auth"]["user"]->ause_position-1;
            $rok = @$this->user_model->insert($params);
        }
        if ($rok === true) {
            $R["result"] = 1;
            $R["message"] = ascii_to_entities("Data have been save.");
        } else {
            $R["result"] = -1;
            $R["error_number"] = $this->db->_error_number();
            $R["error_message"] = $this->db->_error_message();
            $R["message"] = ascii_to_entities("
                    System rejected your data.<br/>
                    Check your data and try again, Please!<br/>
                    ");
            if ($R["error_number"] != 0)
                $R["message"].=ascii_to_entities("<span class='erc'>[" . $R["error_number"] . "] " . $R["error_message"] . ".</span>");
        }
        
        echo json_encode($R);
    }
    function EditPanel($Id = 0) {
        if($Id==0)$Id=(int)$this->input->post('Id');
        if($Id>0)
            $this->assigns->item=$this->user_model->_get($Id);
        $c=$this->load->view('cms/so/01_user/userEditPanel',$this->assigns,true);
        $R["result"] = 1;
        $R["htmlreponse"]=$c;
        $this->output->set_header('Content-type: application/json');
        $this->output->set_output(json_encode($R));
    }
    
}

?>